import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Query.MapQuery;
import model.FileManager;
import model.Visit;

public class main {

	private static List<Visit> my_visits;
	private static FileManager my_fm;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("main running");
		
		
		my_visits = new ArrayList<Visit>();
		my_fm = new FileManager();
	    my_visits = my_fm.loadVisitData();
		
	    //printVisits();
		
		for(Visit visit : my_visits){
			for(int i=0; i < 1; i++){
				JSONObject json = MapQuery.queryAPI(visit);
				try {
					JSONArray rows = json.getJSONArray("rows");
					JSONObject row = rows.getJSONObject(0);
					System.out.println("row" + row.toString());
					
					JSONArray elements = row.getJSONArray("elements");
					JSONObject element = elements.getJSONObject(0);
					
					
					JSONObject distance = element.getJSONObject("distance");
					System.out.println("distance" + distance.toString());

					
					//JSONObject distance = element.getJSONObject("distance");
					visit.setMileage(distance.getInt("value"));
					
					JSONObject duration = element.getJSONObject("duration");
					visit.setTime(duration.getInt("value"));
					
					System.out.println(visit.toString());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		


	}

	public static void printVisits(){
		System.out.println("printVisits running");
		for(Visit visit : my_visits){
			System.out.println(visit.toString() + "\n");
		}
	
	}
    	
}
